package com.waterfairy.permission

import io.flutter.plugin.common.MessageCodec
import org.json.JSONObject
import java.nio.ByteBuffer

/**
 * @user water_fairy
 * @mail 995637517@qq.com
 * @date 2021/12/24 5:58 下午
 * @info
 */
class PermissionResult(
    val requestCode: Int,
    val permission: String,
    val grant: Int
) :
    MessageCodec<Any> {
    override fun encodeMessage(message: Any?): ByteBuffer {
        return ByteBuffer.wrap(toString().toByteArray())
    }

    override fun decodeMessage(message: ByteBuffer?): Any? {
        val json = message?.array()?.let { String(it) }
        json?.let {
            val jsonObject = JSONObject(json)
            return PermissionResult(
                jsonObject.get("requestCode") as Int,
                jsonObject.get("permission") as String,
                jsonObject.get("grant") as Int
            )
        }
        return null;

    }

    override fun toString(): String {
        return "{\"requestCode\":$requestCode,\"permission\":$permission,\"grant\":$grant}";
    }

}